using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Deepcode.Flux.Core.Data;
using Deepcode.Flux.Core.Systems.CMS;

namespace Deepcode.Flux.Core.Systems.CMS.Mappings
{
	/// <summary>
	/// Represents a single domain mapping on the system which links
	/// a domain name URL with a root and homepage node on the system. This
	/// is only used if the URL Re-writing engine is in use.
	/// </summary>
	public class DomainMapping
	{
		// Private field storage
		private int _PK_ID = -1;
		private string _Domain = "";
		private int _FK_Homepage = -1;
		private int _FK_Rootpage = -1;
		
		// Public property accessors
		public int PK_ID { get { return _PK_ID; } set { _PK_ID = value; } }
		public string Domain { get { return _Domain; } set { _Domain = value; } }
		public int FK_Homepage{ get{ return _FK_Homepage; } set{ _FK_Homepage = value; }}
		public int FK_Rootpage{ get{ return _FK_Rootpage; } set{ _FK_Rootpage = value; } }

		#region Linked up properties
		/// <summary>
		/// Returns the content object for the homepage from the current version in vcontrol
		/// </summary>
		public ContentObject Homepage_Current
		{
			get
			{
				ContentManager m = new ContentManager();
				return m.AdxGetByID( FK_Homepage );
			}
		}
		/// <summary>
		/// Returns the content object for the homepage from the current published live version
		/// </summary>
		public ContentObject Homepage_Live
		{
			get
			{
				return ContentQuery.GetByID( FK_Homepage );
			}
		}
		/// <summary>
		/// Returns the content object for the root item from the current version in vcontrol
		/// </summary>
		public ContentObject Rootpage_Current
		{
			get
			{
				ContentManager m = new ContentManager();
				return m.AdxGetByID(FK_Rootpage);
			}
		}
		/// <summary>
		/// Returns the content object for the root item from the current published live version
		/// </summary>
		public ContentObject Rootpage_Live
		{
			get
			{
				return ContentQuery.GetByID( FK_Rootpage );
			}
		}
		/// <summary>
		/// Returns the path of the mapped home node
		/// </summary>
		public string HomePath
		{
			get
			{
				ContentObject o = Homepage_Current;
				if( o == null ) return "Not set";
				return o.NodePath + o.NodeName;
			}
		}
		/// <summary>
		/// Returns the path of the mapped root node
		/// </summary>
		public string RootPath
		{
			get
			{
				ContentObject o = Rootpage_Current;
				if (o == null) return "Not set";
				return o.NodePath + o.NodeName;
			}
		}
		#endregion
		// Default constructor		
		public DomainMapping() { }

		// Constructor loading data
		public DomainMapping(DataRow LoadRow)
		{
			_PK_ID = SQLUtility.DBNull(LoadRow["PK_ID"], -1);
			_Domain = SQLUtility.DBNull(LoadRow["Domain"], "");
			_FK_Homepage = SQLUtility.DBNull(LoadRow["FK_Homepage"], -1);
			_FK_Rootpage = SQLUtility.DBNull(LoadRow["FK_Rootpage"], -1);
		}
	}
}
